//vue3.0中createRouter来创建路由实例
//createWebHashHistory 使用hash模式的路由
//

import { createRouter, createWebHashHistory } from 'vue-router'
import store from '@/store'

const Layout = ()=>import('@/views/Layout')
const Login = ()=>import('@/views/Login')

// 路由规则
const routes = [
 {
  path: '/',
  redirect:'/index',
 },
 {
  path: '/',
  component:Layout,
  children:[
    {
      path:'index',
      component:()=>import('@/views/index')
    },
    {
      path:'supplier',
      component:()=>import('@/views/Supplier')
    },
    {
      path:'goods',
      component:()=>import('@/views/Goods')
    },
    {
      path:'member',
      component:()=>import('@/views/Member')
    },
    {
      path:'staff',
      component:()=>import('@/views/Staff')
    },
  ]
 },
 ,{
  path:'/login',
  component:Login, 
 }
  

]

// 创建路由实例
const router = createRouter({
  // 使用hash方式实现路由
  history: createWebHashHistory(),
  // 配置路由规则，写法和之前的时一样的
  routes
})


// 路由守卫
router.beforeEach((to,from,next) => {
  const {userInfo} = store.state.user
  if(to.path=='/login'){
    // 路径是login 正常执行
    next()
  }else{
    // 判断是否有token
    if(userInfo.token.length == 0){
      // console.log(userInfo.token.length , 'length')
      next('/login')
    }else{
      next()
    }
  }
}
)
export default router
